import PluginBase from "./PluginBase.js";
import * as THREE from "../../../modules/three/three.module.js";
import SprayPathMaker from "../lib/gadget/SprayPathMaker.js";
import SprayPath from "../model/SprayPath.js";
import EditRecord from "../scene/undo/EditRecord.js";

let alphaBaks = [];

export default class AddSprayPathPointPlugin extends PluginBase {
  /**
   * @type {World3d}
   */
  world3d
  workAreaMesh
  /**
   * @type {SprayPathMaker}
   */
  pathMaker
  /**
   * @type {SprayPath}
   */
  sprayPath
  /**
   * 编辑航点时，支持 撤销 和 反撤 功能
   * @type {EditRecord}
   */
  opRecord = new EditRecord()
  // 保存操作栈的序列化对象
  recorderMap = new Map()

  constructor(world3d) {
    super();
    this.world3d = world3d
  }

  attachItem(workAreaMesh, single = false) {
    this.workAreaMesh = workAreaMesh;
    this.pathMaker = new SprayPathMaker(this.opRecord).init(this.world3d, workAreaMesh.vo, single);
    this.world3d.scene.add(this.pathMaker);
    this.loadFromRecorderMap();
    alphaBaks.length = 0;
    workAreaMesh.toggleSelected(false)
    this.pathMaker.areaMeshes.forEach(mesh => {
      alphaBaks.push(mesh.material.opacity);
      mesh.material.opacity = 0.2;
    });
  }

  detachItem() {
    if (!this.workAreaMesh) {
      return;
    }
    this.updateSprayPath();
    this.updateRecorderMap();
    this.world3d.switchCamera(false);
    this.pathMaker.areaMeshes.forEach((mesh, index) => {
      mesh.material.opacity = alphaBaks[index];
    });
    this.pathMaker.recorder = null;
    this.pathMaker.removeFromParent();

    // 注意：这里之所以要注释掉，是因为和 SelectWorkAreaPlugin.detachItem() 冲突了，
    // 如果 SelectWorkAreaPlugin.detachItem() 先执行了，然后再执行AddSprayPathPointPlugin.detachItem()
    // 看起来就好像没有取消选中工作区
    // this.workAreaMesh.material = mtlBak;
    this.workAreaMesh = null;
    this.opRecord.clearAll();
  }

  pointerUpUnmoved(event) {
    const store = this.world3d.store;
    if (!store.stateEditingSprayPath) {
      return;
    }
    if (event.button === 2) { // mouse right button down
      this.detachItem();
      store.freeState();
      this.world3d.switchCamera(false);
      return;
    }
    this.pathMaker.onMouseUp();
  }

  pointerMove(event) {
    if (!this.world3d.store.stateEditingSprayPath) {
      return;
    }
    this.pathMaker.onMouseMove(this.world3d.mouseRaycaster);
  }

  updateSprayPath() {
    if (!this.pathMaker) {
      return
    }
    this.sprayPath = this.pathMaker.createSprayPath();
  }

  updateRecorderMap() {
    const pathMakerId = this.pathMaker.getId();
    const stackUndo = this.pathMaker.saveToRecorderObj();
    this.recorderMap.set(pathMakerId, stackUndo);
  }

  loadFromRecorderMap() {
    const pathMakerId = this.pathMaker.getId();
    const stackUndo = this.recorderMap.get(pathMakerId);
    if (stackUndo) {
      this.pathMaker.loadFromRecorderObj(stackUndo);
    }
  }

  /**
   * 把当前的编辑数据保存为Json对象
   * @return {}
   */
  storageSave() {
    return this.sprayPath.toStorageJson();
  }

  /**
   * 加载json文件得到的对象
   * @param {object} obj
   */
  storageLoad(obj) {
    this.sprayPath = new SprayPath().fromStorageJson(obj);
  }
}
